package com.codeoss.module.stock.service.impl;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.framework.util.DateUtil;
import com.codeoss.module.ir.service.IIrDocTypeService;
import com.codeoss.module.ir.service.IIrSequenceService;
import com.codeoss.module.stock.dao.StockInConfirmMapper;
import com.codeoss.module.stock.model.StockIn;
import com.codeoss.module.stock.model.StockInConfirm;
import com.codeoss.module.stock.model.StockInConfirmLine;
import com.codeoss.module.stock.model.StockInLine;
import com.codeoss.module.stock.service.IStockInConfirmLineService;
import com.codeoss.module.stock.service.IStockInConfirmService;
import com.codeoss.module.stock.service.IStockInLineService;
import com.codeoss.module.stock.service.IStockInService;

@Service
public class StockInConfirmService extends BaseService<StockInConfirmMapper, StockInConfirm>
		implements IStockInConfirmService,ApplicationContextAware {
    private ApplicationContext applicationContext;
    @Autowired
	private IStockInConfirmLineService stockInConfirmLineService;
    @Autowired
	private DateUtil dateUtil;
	@Autowired
	private IIrDocTypeService irDocTypeService;
	@Autowired
	private IIrSequenceService irSequenceService;
	@Override
	public List<StockInConfirm> getConfirmByStockInId(Long stockInId) {
		// TODO Auto-generated method stub
		QueryWrapper<StockInConfirm> query=new QueryWrapper<StockInConfirm>();
		query.eq("stock_in_id", stockInId);
		return list(query);
	}

	@Override
	@Transactional
	public StockInConfirm generateConfirm(Long stockInId) {
		// TODO Auto-generated method stub
		QueryWrapper<StockInConfirm> confirmQuery=new QueryWrapper<StockInConfirm>();
		confirmQuery.eq("stock_in_id", stockInId);
		long count=count(confirmQuery);
		if(count>0) {
			throw new RuntimeException("已经创建 不要重复创建");
		}
		IStockInService stockInService=applicationContext.getBean(IStockInService.class);
		IStockInLineService stockInLineService=applicationContext.getBean(IStockInLineService.class);
		StockIn stockIn=stockInService.getById(stockInId);
		String docStatus=stockIn.getDocStatus();
		if(!"DR".equals(docStatus)) {
			throw new RuntimeException("只有草稿状态 才能创建");
		}
		StockInConfirm stockInConfirm=createFromStockIn(stockIn);
		List<StockInLine> stockInLineList=stockInLineService.getLineListByStockInId(stockInId);
		for(int i=0;i<stockInLineList.size();i++) {
			StockInLine stockInLine=stockInLineList.get(i);
			stockInConfirmLineService.createConfirmLine(stockInConfirm, stockInLine);
		}
		return stockInConfirm;
	}
	
	@Transactional
	protected StockInConfirm createFromStockIn(StockIn stockIn) {
		StockInConfirm stockInConfirm=new StockInConfirm();
		stockInConfirm.setStockInId(stockIn.getId());
		stockInConfirm.setDocStatus("DR");
		stockInConfirm.setDateConfirmed(dateUtil.getSqlNowDatetime());
		Long docTypeId=irDocTypeService.getIrDocTypeIdByCode("stockinconfirm");
		stockInConfirm.setIrDocTypeId(docTypeId);
		stockInConfirm.setDocumentNo(irSequenceService.getNextSequenceNo("stockinconfirm"));
		insert(stockInConfirm);
		return stockInConfirm;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}

	@Override
	@Transactional
	public void prepareIt(Long id) {
		// TODO Auto-generated method stub
		List<StockInConfirmLine> confirmLineList=stockInConfirmLineService.getLineListByStockInConfirmId(id);
		if(confirmLineList==null ||confirmLineList.size()==0) {
			throw new RuntimeException("没有明细");
		}
	}

	@Override
	@Transactional
	public void completeIt(Long id) {
		// TODO Auto-generated method stub
		StockInConfirm stockInConfirm=getById(id);
		stockInConfirm.setDocStatus("CO");
		update(stockInConfirm);
		if(!stockInConfirm.getApproved()) {
			return;
		}
		IStockInLineService stockInLineService=applicationContext.getBean(IStockInLineService.class);
		List<StockInConfirmLine> confirmLineList=stockInConfirmLineService.getLineListByStockInConfirmId(id);
		for(int i=0;i<confirmLineList.size();i++) {
			StockInConfirmLine confirmLine=confirmLineList.get(i);
			Long stockInLineId=confirmLine.getStockInLineId();
			StockInLine stockInLine=stockInLineService.getById(stockInLineId);
			stockInLine.setQtyEntered(confirmLine.getConfirmedQty());
			stockInLineService.update(stockInLine);
		}
	}

	@Override
	@Transactional
	public void approveIt(Long id) {
		// TODO Auto-generated method stub
		StockInConfirm stockInConfirm=getById(id);
		stockInConfirm.setApproved(true);
		update(stockInConfirm);
	}

	@Override
	@Transactional
	public void rejectIt(Long id) {
		// TODO Auto-generated method stub
		StockInConfirm stockInConfirm=getById(id);
		stockInConfirm.setApproved(false);
		update(stockInConfirm);
	}

	@Override
	@Transactional
	public void setDocStatus(Long id, String docStatus) {
		// TODO Auto-generated method stub
		StockInConfirm stockInConfirm=getById(id);
		stockInConfirm.setDocStatus(docStatus);
		update(stockInConfirm);
	}
}